/**$Licence:
 *  Copyright (C) 2008 - 2012 www.java1995.com . All rights reserved.
 *
 *  Use this however/wherever you like, just don't blame me if it breaks anything.
 *
 */

package com.blazemina.config;

import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.service.IoHandler;

import com.blazegame.logging.Logger;
import com.blazegame.logging.LoggerFactory;
import com.blazemina.command.CommandFactory;
import com.blazemina.config.annotation.AnnotationCommandConfig;
import com.blazemina.config.entities.CommandConfig;
import com.blazemina.config.entities.CommandPackages;
import com.blazemina.container.RequestProcessor;
import com.blazemina.executor.BlazeminaExecutors;
import com.blazemina.executor.RouteHelper;
import com.blazemina.mina.config.SocketOption;
import com.blazemina.mina.config.SocketOptionConfig;

/**
 * 统一管理blazemina配置信息
 * 
 * @author JackChu
 * @version 1.0.0 2012-1-11 下午9:05:42
 */
public class BlazeminaConfigManager {
	
	private static final Logger logger = LoggerFactory.getLogger(BlazeminaConfigManager.class);

	private BlazeminaConfig blazeminaConfig;

	private SocketOption socketOption;
	
	private RequestProcessor requestProcessor;

	private IoHandler requestHandler;

	private CommandFactory commandFactory;
	
	private BlazeminaExecutors blazeminaExecutors;
	
	private RouteHelper routeHelper;
	
	private CommandPackages packages = new CommandPackages();
	
	private CommandMappingConfig commandMappingConfig = new AnnotationCommandConfig();
	
	private Map<String, CommandConfig> commandConfigs = new HashMap<String, CommandConfig>();
	
	private static final BlazeminaConfigManager configManager = new BlazeminaConfigManager();
	

	private BlazeminaConfigManager() {

	}

	public static BlazeminaConfigManager me() {
		return configManager;
	}

	/**
	 * 配置文件初始化
	 */
	public void init(BlazeminaConfig blazeminaConfig) {
		this.blazeminaConfig = blazeminaConfig;
		
		initBlazeminaConfig();
		
		initSocketOption();
		
		initCommandConfig();
	}
	
	private void initBlazeminaConfig(){
		requestHandler = blazeminaConfig.configSocketHandler();
		requestProcessor = blazeminaConfig.configRequestProcessor();
		blazeminaExecutors = blazeminaConfig.configExecutor();
		commandFactory = blazeminaConfig.configCommandFactory();
		routeHelper = blazeminaConfig.configRouteHelper();
		blazeminaConfig.configExecutorGroup(blazeminaExecutors);
		blazeminaConfig.configCommandPackages(packages);
	}
	
	/**
	 * 配置socket option
	 */
	private void initSocketOption(){
		SocketOptionConfig socketOptionConfig = new SocketOptionConfig();
		socketOption = socketOptionConfig.configSocketOpention();
		
	}
	
	/**
	 * 初始化command config
	 */
	private void initCommandConfig(){
		commandConfigs = commandMappingConfig.buildCommandMapping();
	}

	/**
	 * @return the requestHandlerHelper
	 */
	public IoHandler getRequestHandler() {
		return requestHandler;
	}

	/**
	 * blazeminaConfig
	 * 
	 * @return the blazeminaConfig
	 */

	public BlazeminaConfig getBlazeminaConfig() {
		return blazeminaConfig;
	}

	/**
	 * @return the socketOption
	 */
	public SocketOption getSocketOption() {
		return socketOption;
	}
	
	/**
	 * @return the requestProcessor
	 */
	public RequestProcessor getRequestProcessor() {
		return requestProcessor;
	}

	/**
	 * @return the routeHelper
	 */
	public RouteHelper getRouteHelper() {
		return routeHelper;
	}

	/**
	 * @return the blazeminaExecutors
	 */
	public BlazeminaExecutors getBlazeminaExecutors() {
		return blazeminaExecutors;
	}
	
	public CommandConfig getCommandMapping(String command){
		CommandConfig commandConfig = commandConfigs.get(command);
		return commandConfig;
	}
	
	/**
	 * @return the commandPackages
	 */
	public Map<String,Boolean> getCommandPackages(){
		return packages.getCommandPackages();
	}

	/**
	 * @return the commandFactory
	 */
	public CommandFactory getCommandFactory() {
		return commandFactory;
	}

	public int getRobotNums() {
		return socketOption.getRobotNums();
	}


	public int getInterval() {
		return socketOption.getInterval();
	}


}
